﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.Web.Crops
{
    public partial class Modify : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    Guid CropsID = new Guid(Request.Params["id"]);
                    BindFram();
                    BindCropsCategory();
                    ShowInfo(CropsID);
                }
            }
        }

        protected void BindFram()
        {
            Things.BLL.Farm farmerBLL = new BLL.Farm();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" IsDeleted=0 ");
            ds = farmerBLL.GetList(strWhere.ToString());
            Things.Common.UICommon.BindDDL(ddlFarm, ds, "Address", "FarmID");
        }

        protected void BindCropsCategory()
        {
            Things.BLL.CropsCategory cropsCategoryBLL = new BLL.CropsCategory();
            DataSet ds = new DataSet();
            ds = cropsCategoryBLL.GetAllList();
            Things.Common.UICommon.BindDDL(ddlCropsCategory, ds, "CropsName,Kind", Things.Common.ConstantString.Seperate, "CropsID");
        }

        private void ShowInfo(Guid CropsID)
        {
            Things.BLL.Crops bll = new Things.BLL.Crops();
            Things.Model.Crops model = bll.GetModel(CropsID);
            this.lblCropsID.Text = model.CropsID.ToString();
            this.txtPlanDate.Text = model.PlanDate.ToString("yyyy-MM-dd");
            this.chkIsProducted.Checked = model.IsProducted;
            Things.Common.UICommon.SetSelectedDDL(ddlCropsCategory, model.CropsCategoryID.ToString());
            Things.Common.UICommon.SetSelectedDDL(ddlFarm, model.FarmID.ToString());

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.ddlFarm.SelectedValue == "-1")
            {
                strErr += "请选择产地！\\n";
            }
            if (this.ddlCropsCategory.SelectedValue == "-1")
            {
                strErr += "请农产品名称！\\n";
            }
            if (!PageValidate.IsDateTime(txtPlanDate.Text))
            {
                strErr += "种植时间格式错误！\\n";
            }

            Guid cropsCategoryID = Guid.Empty;
            if (Guid.TryParse(this.ddlCropsCategory.SelectedValue, out cropsCategoryID) == false)
            {
                strErr += "农产品名称信息有误！\\n";
            }

            Guid farmID = Guid.Empty;
            if (Guid.TryParse(this.ddlFarm.SelectedValue, out farmID) == false)
            {
                strErr += "产地信息有误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            Guid CropsID = new Guid(this.lblCropsID.Text);
            DateTime PlanDate = DateTime.Parse(this.txtPlanDate.Text);
            bool IsDeleted = false;
            string tempCropsName = this.ddlCropsCategory.SelectedItem.Text;
            string CropsName = tempCropsName.Substring(0, tempCropsName.IndexOf(Things.Common.ConstantString.Seperate));
            bool IsProducted = this.chkIsProducted.Checked;
            string farmAddress = ddlFarm.SelectedItem.Text;
            Things.BLL.CropsCategory cropsBLL = new BLL.CropsCategory();
            Things.Model.CropsCategory crops = cropsBLL.GetModel(cropsCategoryID);
            string CropsKind = crops != null ? crops.Kind : string.Empty;


            Things.Model.Crops model = new Things.Model.Crops();
            model.CropsID = CropsID;
            model.PlanDate = PlanDate;
            model.IsDeleted = IsDeleted;
            model.CropsName = CropsName;
            model.IsProducted = IsProducted;
            model.CropsCategoryID = cropsCategoryID;
            model.FarmID = farmID;
            model.FarmAddress = farmAddress;
            model.CropsKind = CropsKind;

            Things.BLL.Crops bll = new Things.BLL.Crops();
            bll.Update(model);
            Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
